1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.io;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import com.google.common.annotations.GwtCompatible;
20
21 import java.io.IOException;
22
23
24
25
26
27
28
29
30 @GwtCompatible(emulated = true)
31 final class GwtWorkarounds {
32 private GwtWorkarounds() {}
33
34
35
36
37 interface CharInput {
38 int read() throws IOException;
39 void close() throws IOException;
40 }
41
42
43
44
45 static CharInput asCharInput(final CharSequence chars) {
46 checkNotNull(chars);
47 return new CharInput() {
48 int index = 0;
49
50 @Override
51 public int read() {
52 if (index < chars.length()) {
53 return chars.charAt(index++);
54 } else {
55 return -1;
56 }
57 }
58
59 @Override
60 public void close() {
61 index = chars.length();
62 }
63 };
64 }
65
66
67
68
69 interface ByteInput {
70 int read() throws IOException;
71 void close() throws IOException;
72 }
73
74
75
76
77 interface ByteOutput {
78 void write(byte b) throws IOException;
79 void flush() throws IOException;
80 void close() throws IOException;
81 }
82
83
84
85
86 interface CharOutput {
87 void write(char c) throws IOException;
88 void flush() throws IOException;
89 void close() throws IOException;
90 }
91
92
93
94
95
96 static CharOutput stringBuilderOutput(int initialSize) {
97 final StringBuilder builder = new StringBuilder(initialSize);
98 return new CharOutput() {
99
100 @Override
101 public void write(char c) {
102 builder.append(c);
103 }
104
105 @Override
106 public void flush() {}
107
108 @Override
109 public void close() {}
110
111 @Override
112 public String toString() {
113 return builder.toString();
114 }
115 };
116 }
117 }
118